package thread.syn;

import org.openjdk.jol.info.ClassLayout;

public class BiasLock {
  public static void main(String[] args) {

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    Object lockObj = new Object();
 // 必须设置 开启偏向锁 -XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
    new Thread(() -> {
      synchronized (lockObj) {
        System.out.println(ClassLayout.parseInstance(lockObj).toPrintable()); // code 2
        //biased_lock偏向锁标识位+lock锁状态标识位=000，轻量级锁状态。
      }
    }).start();

    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

//  code 1

    new Thread(() -> {
      synchronized (lockObj) {
        System.out.println(ClassLayout.parseInstance(lockObj).toPrintable()); // code 2
        //biased_lock偏向锁标识位+lock锁状态标识位=000，轻量级锁状态。
      }
    }).start();

  }

}
